package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;

/**
 * @author Gareth Cao
 * @date 2022/4/28 9:33
 */
@Slf4j
@Component//将对象交给spring容器管理
public class MyMetaObjectHandler implements MetaObjectHandler {
    //添加时自动执行该方法
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("元数据对象metaObject: {} ", metaObject);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createTime", LocalDateTime.now());
        //静态方法获取数据
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    //修改时自动执行该方法
    @Override
    public void updateFill(MetaObject metaObject) {
        //打印当前线程的id
        log.info("当前线程的id: {}", Thread.currentThread().getId());
        log.info("元数据对象metaObject: {} ", metaObject);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }


}
